Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

488
Views
What is the right way to use `useFactory` and `deps:[ ]` in Angular?

I want to use a simple provider with useFactory which will also use deps (learning purpose) .

So I've created this plnkr :

//app.module.ts
...
const randomFactory = (car,engine) => { return new Calc(car,engine);  };
 ...
 providers: [Car,Engine, 
             { provide: 'Random', 
               useFactory: randomFactory ,  
               deps: [Car, Engine],
             },
            ]

Where Calc is :

export class Calc
{
  car:Car;
  engine:Engine;

  constructor ( Engine engine, Car car)
   {
    this.car=car;
    this.engine=engine;
   }
  doSomething(){alert(this.car.getInfo() + this.engine.getInfo()}
}

Now I want to use it as an injected object so I did this :

export class AppComponent {
  constructor(@Inject('Random') r) {
    r.doSomething(); //should alert "car data + engine data" , but I get an error.
   }
}

But I get an error : car is not defined

Question:

I don't see any point of using deps:[] If I already declare that modules in the main app module ( and so it is known in the global app) and in the Calc class where I import those classes.

So how can I fix my code to use deps:[] the right way ?

plnkr

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The providers configuration is correct. You've got a syntax error in the Calc constructor. The correct declaration of the arguments is as follows:

constructor (engine: Engine, car: Car){

Here is the updated plunker:

https://plnkr.co/edit/HsECR0HEcIaPNbK6A7ZZ?p=preview

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!